package leo.mystudy.executors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author lichao email:lichao@witspring.com
 * @Description: 缓存线程池
 * @Date 2018/4/23 18:38
 * @since Ver V0.0.1
 */
public class CachedThreadPool {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < 10; i++) {
            exec.execute(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("启动子线程：" + Thread.currentThread().getName());
                    Thread.sleep(2000L);
                    //System.out.println("关闭子线程：" + Thread.currentThread().getName());
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        exec.shutdown();
        while (true) {
            if (exec.isTerminated()) {
                System.out.println("主程序完成");
                break;
            }
        }

    }
}
